package org.niux.aims.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.models.Entitys;
import org.niux.aims.service.IEntitysService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.vo.AccountVO;
import org.niux.aims.vo.ApplicationVO;
import org.niux.aims.vo.EntityTypeVO;
import org.niux.aims.vo.EntityVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：EntityManagerAction.java<br>
 * 摘    要：实体管理页面类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-8-6<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class EntityManagerAction extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5303091579071288886L;
	
	private Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="entityService")
	private IEntitysService entityService;
	
	@Resource(name="logsService")
	private ILogsService logsService;
	
	private int entityCount;
	private String entityName;

	/**
	 * 
	 * 方法概述：页面渲染
	 * 描述：页面渲染
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-6
	 */
	public String showPage(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】进入实体管理界面");
		entityCount = entityService.findByParent(null).size();
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：搜索实体
	 * 描述：搜索实体
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-8-25
	 */
	public String searchEntity(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】查询实体【"+entityName+"】");
		
		log.debug("搜索实体："+entityName);
		
		List<Entitys> allEntitys = entityService.findByEntityName(entityName);
		List<EntityVO> entitys = new ArrayList<EntityVO>();
		
		for (Entitys entity : allEntitys) {
			if(entity.getEntitys() == null){
				EntityVO menu = transformToVo(entity);
				entitys.add(menu);
			}
		}
		
		entityCount = entitys.size();
		
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：DO转换成VO
	 * 描述：DO转换成VO
	 * @param entity
	 * @return
	 * 返回类型：EntityVO
	 * 修改人：梁铭显
	 * 修改时间：2013-8-25
	 */
	public EntityVO transformToVo(Entitys entity){
		EntityVO entityVo = new EntityVO();
		entityVo.setId(Integer.toString(entity.getId()));
		entityVo.setEntityName(entity.getEntityName());
		entityVo.setEntityCode(entity.getEntityCode());
		entityVo.setParent(entity.getEntitys() == null ? null : Integer.toString(entity.getEntitys().getId()));
		
		ApplicationVO appVo = new ApplicationVO();
		appVo.setId(entity.getApplication().getId());
		appVo.setAppName(entity.getApplication().getAppName());
		
		entityVo.setAppVo(appVo);
		
		EntityTypeVO entityTypeVo = new EntityTypeVO();
		entityTypeVo.setId(entity.getEntityType().getId());
		entityTypeVo.setEntityTypeName(entity.getEntityType().getEntityType());
		
		entityVo.setEntityTypeVo(entityTypeVo);
		
		return entityVo;
	}
	
	public int getEntityCount() {
		return entityCount;
	}

	public void setEntityCount(int entityCount) {
		this.entityCount = entityCount;
	}

	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}
	
}
